#include "task_scheduler.h"

Timer_t Timer1ms = {
    .bEnable = true,
    .hCounter = MIDDLE_FREQUENCY_TASK_PERIOD,
    .hCompare = MIDDLE_FREQUENCY_TASK_PERIOD,
    .bFlag = false,
};

Timer_t Timer10ms = {
    .bEnable = true,
    .hCounter = LOW_FREQUENCY_TASK_PERIOD,
    .hCompare = LOW_FREQUENCY_TASK_PERIOD,
    .bFlag = false,
};

Timer_t Timer500ms = {
    .bEnable = true,
    .hCounter = 1000,
    .hCompare = 1000,
    .bFlag = false,
};

Timer_t TimerGather = {
    .bEnable = true,
    .hCounter = 10,
    .hCompare = 10,
    .bFlag = false,
};

Timer_t TimerDiscrete = {
    .bEnable = false,
    .hCounter = 0,
    .hCompare = 0,
    .bFlag = false,
};

SystemTimer_t SystemTimer = {
    .oTimerNum = 5,
    .pTimer = {
        &Timer1ms,
        &Timer10ms,
        &Timer500ms,
        &TimerGather,
        &TimerDiscrete,
    }
};

void SystemTimerUpdate(void)
{
    u8 i = 0;
    for(i = 0;i < SystemTimer.oTimerNum; i++)
    {
        if(SystemTimer.pTimer[i]->bEnable)
        {
            SystemTimer.pTimer[i]->hCounter--;
            if(SystemTimer.pTimer[i]->hCounter == 0)
            {
                SystemTimer.pTimer[i]->bFlag = true;
            }
        }
    }
}
extern void BeepTask();
void TaskScheduler(void)
{
		static uint8_t BeepCnt;
		extern uint8_t FanSartCnt;
    if(Timer1ms.bFlag)   //2ms
    {
        Timer1ms.bFlag = false;
        Timer1ms.hCounter = Timer1ms.hCompare;
//        (void)MC_GetVeloRef();
        MiddleFreqTask();
        FiniteStateMachine();
			
				Adv_Protect();
				//SleepMcu(); 20250429
    }
    
    if(Timer10ms.bFlag)  //1ms;
    {
        Timer10ms.bFlag = false;
        Timer10ms.hCounter = Timer10ms.hCompare;
        LowFreqTask();		
    }
    
    if(Timer500ms.bFlag)
    {
        Timer500ms.bFlag = false;
        Timer500ms.hCounter = Timer500ms.hCompare;
			
			  FlagACOn=0;
				extern void FlashTime();
				FlashTime();
				extern void ziranmode();
				ziranmode();
        if(OnOffTime.status == 1)
        {
            if(OnOffTime.timeCnt < OnOffTime.timeEnd)
                OnOffTime.timeCnt++;
            else
            {
							  PWMDuty.FanON    =0;
							  PWMDuty.LedON    = 0;
                OnOffTime.status = 0;
                Command = CMD_STOP;
                RELAY_OFF;
                FAN_Shake   = 0;
                ZiRanFan    = 0;
            }
        }
    }
		
		//-----------------------------------------
		if(TimerGather.bFlag) //5ms
		{
			TimerGather.bFlag = false;
			TimerGather.hCounter = TimerGather.hCompare;
//			PWM_INCR();
			BeepTask();
			if(FanSartCnt>0)
			{
				FanSartCnt--;
				if(FanSartCnt == 0)
				{
					PWMDuty.FanON=1;
					Command = CMD_START;
					FAN_Cmd = Command;
					SignalChain.hPowref = LEVEL3_POWER_CMD;
				}
			}
		}
//		if(BeepCnt++>25)
//		{
//			BeepCnt = 0;
//			//beeon_pwm(0);
//		}
}


